package com.auxgroup.smarthome.app.entity;

import com.alibaba.fastjson.annotation.JSONField;

import java.time.LocalDateTime;
import java.time.ZoneId;

/**
 * 峰谷表
 * Created by niuGuangzhe on 2017/8/1.
 * kevin 缓存到redis的 峰谷时间配置实体
 */
public class AppPeakValleyRedisEntity  {
    //波峰开始小时
    private Integer peakStartHour;
    //波峰结束小时
    private Integer peakEndHour;
    //波谷开始小时
    private Integer valleyStartHour;
    //波谷结束小时
    private Integer valleyEndHour;
    //机智云产品productKey
    private String pk;
    //机智云产品did
    private String did;

    public AppPeakValleyRedisEntity() {}

    public AppPeakValleyRedisEntity(String pk, String did) {
        this.peakStartHour = 0;
        this.peakEndHour = 0;
        this.valleyStartHour = 0;
        this.valleyEndHour = 0;
        this.pk = pk;
        this.did = did;
    }

    public Integer getPeakStartHour() {
        return peakStartHour;
    }

    public void setPeakStartHour(Integer peakStartHour) {
        this.peakStartHour = peakStartHour;
    }

    public Integer getPeakEndHour() {
        return peakEndHour;
    }

    public void setPeakEndHour(Integer peakEndHour) {
        this.peakEndHour = peakEndHour;
    }

    public Integer getValleyStartHour() {
        return valleyStartHour;
    }

    public void setValleyStartHour(Integer valleyStartHour) {
        this.valleyStartHour = valleyStartHour;
    }

    public Integer getValleyEndHour() {
        return valleyEndHour;
    }

    public void setValleyEndHour(Integer valleyEndHour) {
        this.valleyEndHour = valleyEndHour;
    }

    public String getPk() {
        return pk;
    }

    public void setPk(String pk) {
        this.pk = pk;
    }

    public String getDid() {
        return did;
    }

    public void setDid(String did) {
        this.did = did;
    }

    /**
     * 波峰时间段、波谷时间段在同一天有可能存在两个时间段
     * 若波峰有两个时间段，则波谷就只有一个时间段 故时间段格式为 num1,num2_num3,num4|num5,num6
     * 若波谷有两个时间段，则波峰就只有一个时间段 故时间段格式为 num1,num2|num3,num4_num5,num6
     * "|"竖线的左边为波峰  右边为波谷
     * @return
     */
    @JSONField(serialize = false)
    public String getTimeBucket(){
        StringBuffer sb = new StringBuffer();
        if (peakEndHour != 0 && peakStartHour > peakEndHour){
            sb.append(peakStartHour).append(",").append(0).append("_")
                    .append("0").append(",").append(peakEndHour);
        }else{
            sb.append(peakStartHour).append(",").append(peakEndHour);
        }

        if (valleyEndHour != 0 && valleyStartHour > valleyEndHour ){
            sb.append("|").append(valleyStartHour).append(",").append(0).append("_")
                    .append("0").append(",").append(valleyEndHour);
        }else{
            sb.append("|").append(valleyStartHour).append(",").append(valleyEndHour);
        }
        return sb.toString();
    }

    /**
     * 1:波谷开启 2：波峰开启 0或3均为关闭
     * 达到波峰开启时间 发送 指令 1
     * 需求修改 2017-11-08 要求必须在波峰波谷开启的时候 每10分钟发起开启指令
     * @return
     */
    public boolean reachPeakStartHour(){
        if(isReachPeakCondition()) return false;
        LocalDateTime localDateTime = LocalDateTime.now(ZoneId.of("Asia/Shanghai"));
        int hour = localDateTime.getHour();
        if (peakEndHour != 0 && peakStartHour > peakEndHour){
            return (0 <= hour && hour< peakEndHour) ||( hour >= peakStartHour && hour <= 23) ;
        }else{
            return peakEndHour==0 ? ( hour >= peakStartHour && hour <= 23 )
                    : (hour >= peakStartHour && hour < peakEndHour);
        }

    }

    /**
     * 1:波谷开启 2：波峰开启 0或3均为关闭
     * 达到波谷开启时间 发送，指令 2
     * 需求修改 2017-11-08 要求必须在波峰波谷开启的时候 每10分钟发起开启指令
     * @return
     */
    public boolean reachValleyStartHour(){
        if(isReachValleyCondition()) return false;
        LocalDateTime localDateTime = LocalDateTime.now(ZoneId.of("Asia/Shanghai"));
        int hour = localDateTime.getHour();
        if (valleyEndHour != 0 && valleyStartHour > valleyEndHour){
            return (0 <= hour && hour < valleyEndHour) ||( hour >= valleyStartHour && hour <= 23) ;
        }else{
            return valleyEndHour==0 ? (hour >= valleyStartHour && hour <= 23 )
                    : (hour >= valleyStartHour && hour < valleyEndHour);
        }
    }

    /**
     * 是否关闭波谷
     * @return
     */
    private boolean isReachValleyCondition() {
        return valleyStartHour.equals(valleyEndHour) || (valleyStartHour==0 && valleyEndHour==0);
    }

    /**
     * 是否关闭波峰
     * @return
     */
    private boolean isReachPeakCondition() {
        return (peakStartHour.equals(peakEndHour)) || (peakStartHour==0 && peakEndHour==0);
    }

    /**
     * 检测峰谷节电是否处于关闭状态
     * @return
     */
    public boolean isCloseState() {
        return peakStartHour==0 && peakEndHour==0 && valleyStartHour==0 && valleyEndHour==0;
    }

    /**
     * 发送关闭指令 0或3
     * @return
     */
    public boolean closeTime(){
        if(isReachValleyCondition() || isReachPeakCondition()) return true;
        LocalDateTime localDateTime = LocalDateTime.now(ZoneId.of("Asia/Shanghai"));
        int hour = localDateTime.getHour();
        if ( ( hour == peakEndHour && hour != valleyStartHour ) ||
                ( hour == valleyEndHour && hour != peakStartHour ) ){
            return true;
        }
        return false;
    }

}
